<?php
namespace Common\Model;
use Think\Model;
class AccountModel extends Model{
	public $autoCheckFields = false;

	public function filter_field($data){
		//账户类型
		$data['account_type_name'] = get_type_desc($data['account_type'], 'account_type_desc');
		//账单类型
		$data['bill_type_name']    = get_type_desc($data['bill_type'], 'bill_type_desc');
		//价格描述
		$data['money_desc']  = ($data['money'] > 0 ? '+' : '').$data['money'];
		//价格颜色
		$data['money_color'] = $data['money'] > 0 ? '#ff4c26' : '#6aaf66';
		return $data;
	}
	/**
	 * 账户充值
	 * @param  [type] $user_id  用户ID
	 * @param  [type] $pay_type 支付类型
	 * @param  string $money    金额
	 * @param  string $order_sn 订单号
	 */
	public function recharge($user_id, $pay_type, $money = '', $order_sn = ''){
		if(!$user_id || !$pay_type || $money < 0 || !$order_sn){
			return false;
		}
		//充值到账户
		$this->update_account_money($user_id, $money, 'usable_account');
		//添加账单
		$this->add_bill($user_id, 'recharge', $money, 'usable_account', $pay_type, $order_sn);

		return true;
	}
	/**
	 * 提现
	 * @param  [type] $user_id 用户ID
	 * @param  string $money   提现金额
	 */
	public function take_out($user_id, $money = ''){
		if(!$user_id || !$money){
			return false;
		}
		//判断账户余额是否够提现
		$usable_money = D('Account')->get_account_money($user_id, 'usable_account');
		if($usable_money < $money){
			return false;
		}
		//扣除可用账户金额
		$this->update_account_money($user_id, -$money, 'usable_account');
		//添加扣除可用账户记录
		$this->add_bill($user_id, 'take_out', -$money, 'usable_account');
		//添加已用账户增加记录
		$this->add_bill($user_id, 'take_out', $money, 'used_account');

		return true;
	}
	
	
	/**
	 * 分享文章获取金额
	 * @param  [type] $share_user_id   分享用户ID
	 * @param  [type] $article_user_id 文章用户ID
	 * @param  [type] $article_id      文章ID
	 * @param  [type] $money           分享金额
	 */
	public function pay_order($pay_type = 'alipay', $user_id, $order_sn, $agreement_id = '', $money, $remark = ''){
		if(!$user_id || !$order_sn || $money < 0){
			return false;
		}

		//用户冻结账户增加金额
		$this->update_account_money($user_id, $money, 'blocked_account');
		//添加冻结账户账单
		$this->add_bill($user_id, 'pay_agreement_order', $money, 'blocked_account', $pay_type, $order_sn, $agreement_id, $remark);

		return true;
	}
	/**
	 * 取消订单
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $order_sn     订单号
	 * @param  [type] $agreement_id 合同ID
	 * @param  [type] $money        订单金额
	 * @param  string $remark       备注
	 */
	public function cancel_order($user_id, $order_sn, $agreement_id, $money, $remark = ''){
		if(!$user_id || !$order_sn || $money < 0){
			return false;
		}

		//扣除用户冻结账户金额
		$this->update_account_money($user_id, -$money, 'blocked_account');
		//添加扣除冻结账户账单
		$this->add_bill($user_id, 'cancel_agreement_order', -$money, 'blocked_account', '', $order_sn, $agreement_id, $remark);

		//可用账户增加金额
		$this->update_account_money($user_id, $money, 'usable_account');
		//添加账单
		$this->add_bill($user_id, 'cancel_agreement_order', $money, 'usable_account', '', $order_sn, $agreement_id, $remark);

		return true;
	}
	/**
	 * 开通账户
	 * @param  [type] $user_id      用户ID
	 * @param  string $account_type 账户类型
	 */
	public function create_account($user_id, $account_type = 'usable_account'){
		if(!$user_id || !$account_type){
			return false;
		}
		if(!$this->is_have_account($user_id, $account_type)){
			$data['user_id']      = $user_id;
			$data['account_type'] = $account_type;
			$data['create_time']  = time();
			D('UserAccount')->add($data);
		}

		return true;
	}
	/**
	 * 判断是否开通账户
	 * @param  [type]  $user_id      用户ID
	 * @param  string  $account_type 账户类型
	 */
	public function is_have_account($user_id, $account_type = 'usable_account'){
		$where['user_id']      = $user_id;
		$where['account_type'] = $account_type;
		$count = D('UserAccount')->where($where)->count();

		return $count > 0;
	}
	/**
	 * 获取账户金额
	 * @param  [type] $user_id      用户ID
	 * @param  string $account_type 账户类型
	 */
	public function get_account_money($user_id, $account_type = 'usable_account'){
		$where['user_id']      = $user_id;
		$where['account_type'] = $account_type;
		$money = D('UserAccount')->where($where)->getField('money');

		return $money ? $money : 0;
	}
	
	/**
	 * 更新账户金额
	 * @param  [type] $user_id      用户ID
	 * @param  [type] $account_type 账户类型
	 * @param  [type] $money        金额
	 */
	private function update_account_money($user_id, $money, $account_type = 'usable_account'){
		if(!$this->create_account($user_id, $account_type)){
			return false;
		}
		
		if($money > 0){
			$data['money'] = array('exp','money+' . abs($money));
		}else{
			$data['money'] = array('exp','money-' . abs($money));
		}
		$data['edit_time'] = time();
		
		$where['user_id']      = $user_id;
		$where['account_type'] = $account_type;
		D('UserAccount')->where($where)->save($data);

		return true;
	}
	/**
	 * 添加账单
	 * @param [type] $user_id      用户ID
	 * @param [type] $bill_type    账单类型
	 * @param [type] $money        账单金额
	 * @param string $account_type 账户类型
	 * @param string $pay_type     支付类型
	 * @param string $order_sn     订单号
	 */
	private function add_bill($user_id, $bill_type, $money, $account_type = 'usable_account', $pay_type = 'weixin', $order_sn ='', $agreement_id = '', $remark = ''){
		$city_info = D('User')->get_user_city_info($user_id);
		$data['province_id']  = $city_info['province_id'];
		$data['city_id']      = $city_info['city_id'];
		$data['user_id']      = $user_id;
		$data['money']        = $money;
		$data['bill_type']    = $bill_type;
		$data['account_type'] = $account_type;
		$data['balance']      = $this->get_account_money($user_id, $account_type);
		$data['order_sn']     = $order_sn ? $order_sn : '';
		$data['agreement_id'] = $agreement_id ? $agreement_id : 0;
		$data['pay_type']     = $pay_type ? $pay_type : 'system';
		$data['app_type']     = 'web';
		$data['remark']       = $remark ? $remark : '';
		$data['create_time']  = time();

		D('UserBill')->add($data);

		return D('UserBill')->getLastInsID();
	}



}